<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Event;

class Message extends Base
{
    protected $name = 'message';


    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function getImagesTextAttr($value, $data)
    {
        return $data['images'] ? explode(',', $data['images']) : '';
    }

    public static function _save($param, $options): array
    {
        try {
            $param['status'] = 2;
            $param['admin_id'] = $options['admin']->id;
            $param['admin_name'] = $options['admin']->admin_name;
            $param['reply_time'] = time();

            parent::update($param);
            return ['code' => 1, 'msg' => '回复成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function add($param, $user): array
    {
        try {

            parent::create([
                'uid'     => $user->id,
                'code'    => $user->code,
                'title'   => $param['content'],
                'type'    => '意见反馈',
                'comment' => $param['content'],
                'images'  => is_array($param['images']) ? implode(',', $param['images']) : $param['images'],
            ]);

            return ['code' => 1, 'msg' => '感谢您的反馈'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
